import 'package:hive_ce/hive.dart';

part 'server_model.g.dart';

/// 服务器信息模型
@HiveType(typeId: 0)
class ServerModel {
  @HiveField(0)
  String id;

  @HiveField(1)
  String name;

  @HiveField(2)
  String host;

  @HiveField(3)
  int port;

  @HiveField(4)
  String username;

  @HiveField(5)
  String? password;

  @HiveField(6)
  String? privateKey;

  @HiveField(7)
  DateTime createdAt;

  @HiveField(8)
  DateTime? lastConnectedAt;

  @HiveField(9)
  ServerStatus status;

  @HiveField(10)
  String? tags;

  @HiveField(11)
  String? groupId;  // 分组 ID

  ServerModel({
    required this.id,
    required this.name,
    required this.host,
    required this.port,
    required this.username,
    this.password,
    this.privateKey,
    required this.createdAt,
    this.lastConnectedAt,
    this.status = ServerStatus.disconnected,
    this.tags,
    this.groupId,  // 分组 ID
  });

  // 复制方法
  ServerModel copyWith({
    String? id,
    String? name,
    String? host,
    int? port,
    String? username,
    String? password,
    String? privateKey,
    DateTime? createdAt,
    DateTime? lastConnectedAt,
    ServerStatus? status,
    String? tags,
    String? groupId,
  }) {
    return ServerModel(
      id: id ?? this.id,
      name: name ?? this.name,
      host: host ?? this.host,
      port: port ?? this.port,
      username: username ?? this.username,
      password: password ?? this.password,
      privateKey: privateKey ?? this.privateKey,
      createdAt: createdAt ?? this.createdAt,
      lastConnectedAt: lastConnectedAt ?? this.lastConnectedAt,
      status: status ?? this.status,
      tags: tags ?? this.tags,
      groupId: groupId ?? this.groupId,
    );
  }

  // 获取连接字符串
  String get connectionString => '$username@$host:$port';
}

/// 服务器状态
@HiveType(typeId: 1)
enum ServerStatus {
  @HiveField(0)
  connecting,

  @HiveField(1)
  connected,

  @HiveField(2)
  disconnected,

  @HiveField(3)
  error;

  String get displayName {
    switch (this) {
      case ServerStatus.connecting:
        return '连接中';
      case ServerStatus.connected:
        return '已连接';
      case ServerStatus.disconnected:
        return '已断开';
      case ServerStatus.error:
        return '错误';
    }
  }
}

/// 服务器分组模型
@HiveType(typeId: 2)
class ServerGroup {
  @HiveField(0)
  String id;

  @HiveField(1)
  String name;

  @HiveField(2)
  String? description;

  @HiveField(3)
  DateTime createdAt;

  @HiveField(4)
  int sortOrder;  // 排序顺序

  ServerGroup({
    required this.id,
    required this.name,
    this.description,
    required this.createdAt,
    this.sortOrder = 0,
  });

  ServerGroup copyWith({
    String? id,
    String? name,
    String? description,
    DateTime? createdAt,
    int? sortOrder,
  }) {
    return ServerGroup(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      createdAt: createdAt ?? this.createdAt,
      sortOrder: sortOrder ?? this.sortOrder,
    );
  }
}

/// 服务器监控数据
class ServerStatusData {
  final String serverId;
  final DateTime timestamp;

  // CPU
  final double cpuUsage;
  final String? cpuModel;
  final int? cpuCores;

  // 内存
  final int totalMemory;
  final int usedMemory;
  final int availableMemory;
  final double memoryUsage;

  // 磁盘
  final int totalDisk;
  final int usedDisk;
  final int availableDisk;
  final double diskUsage;

  // 网络
  final int networkRxBytes;
  final int networkTxBytes;
  final double networkRxSpeed;
  final double networkTxSpeed;

  // 系统信息
  final String? osVersion;
  final String? hostname;
  final String? uptime;
  final double? loadAverage1;
  final double? loadAverage5;
  final double? loadAverage15;

  ServerStatusData({
    required this.serverId,
    required this.timestamp,
    required this.cpuUsage,
    this.cpuModel,
    this.cpuCores,
    required this.totalMemory,
    required this.usedMemory,
    required this.availableMemory,
    required this.memoryUsage,
    required this.totalDisk,
    required this.usedDisk,
    required this.availableDisk,
    required this.diskUsage,
    required this.networkRxBytes,
    required this.networkTxBytes,
    required this.networkRxSpeed,
    required this.networkTxSpeed,
    this.osVersion,
    this.hostname,
    this.uptime,
    this.loadAverage1,
    this.loadAverage5,
    this.loadAverage15,
  });

  // 默认空数据
  factory ServerStatusData.empty(String serverId) {
    return ServerStatusData(
      serverId: serverId,
      timestamp: DateTime.now(),
      cpuUsage: 0,
      totalMemory: 0,
      usedMemory: 0,
      availableMemory: 0,
      memoryUsage: 0,
      totalDisk: 0,
      usedDisk: 0,
      availableDisk: 0,
      diskUsage: 0,
      networkRxBytes: 0,
      networkTxBytes: 0,
      networkRxSpeed: 0,
      networkTxSpeed: 0,
    );
  }
}

